package handler

import (
	"strconv"
	"yanxue_ai_go/pkg/common/response"
	"yanxue_ai_go/services/agent/internal/service"

	"github.com/gin-gonic/gin"

	"go.uber.org/zap"

	"yanxue_ai_go/pkg/common/logger"
)

type AgentHandler struct {
	agentService *service.AgentService
}

func NewAgentHandler(agentService *service.AgentService) *AgentHandler {
	return &AgentHandler{
		agentService: agentService,
	}
}

// CreateAgent 创建代理
func (h *AgentHandler) CreateAgent(c *gin.Context) {
	var req service.CreateAgentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, err.Error())
		return
	}

	agent, err := h.agentService.CreateAgent(c.Request.Context(), &req)
	if err != nil {
		logger.Logger.Error("Failed to create agent", zap.Error(err))
		response.InternalServerError(c, "Failed to create agent")
		return
	}

	response.Success(c, agent)
}

// GetAgent 获取代理详情
func (h *AgentHandler) GetAgent(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "Invalid agent ID")
		return
	}

	agent, err := h.agentService.GetAgent(c.Request.Context(), uint(id))
	if err != nil {
		response.NotFound(c, "Agent not found")
		return
	}

	response.Success(c, agent)
}

// ListAgents 获取代理列表
func (h *AgentHandler) ListAgents(c *gin.Context) {
	var req service.ListAgentsRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.BadRequest(c, err.Error())
		return
	}

	// 设置默认值
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.PageSize = 10
	}

	agents, total, err := h.agentService.ListAgents(c.Request.Context(), &req)
	if err != nil {
		logger.Logger.Error("Failed to list agents", zap.Error(err))
		response.InternalServerError(c, "Failed to list agents")
		return
	}

	response.Success(c, gin.H{
		"agents":    agents,
		"total":     total,
		"page":      req.Page,
		"page_size": req.PageSize,
	})
}

// UpdateAgent 更新代理
func (h *AgentHandler) UpdateAgent(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "Invalid agent ID")
		return
	}

	var req service.UpdateAgentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, err.Error())
		return
	}

	agent, err := h.agentService.UpdateAgent(c.Request.Context(), uint(id), &req)
	if err != nil {
		logger.Logger.Error("Failed to update agent", zap.Error(err))
		response.InternalServerError(c, "Failed to update agent")
		return
	}

	response.Success(c, agent)
}

// DeleteAgent 删除代理
func (h *AgentHandler) DeleteAgent(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "Invalid agent ID")
		return
	}

	if err := h.agentService.DeleteAgent(c.Request.Context(), uint(id)); err != nil {
		logger.Logger.Error("Failed to delete agent", zap.Error(err))
		response.InternalServerError(c, "Failed to delete agent")
		return
	}

	response.Success(c, gin.H{"message": "Agent deleted successfully"})
}

// ExecuteAgent 执行代理
func (h *AgentHandler) ExecuteAgent(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "Invalid agent ID")
		return
	}

	var req service.ExecuteAgentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, err.Error())
		return
	}

	execution, err := h.agentService.ExecuteAgent(c.Request.Context(), uint(id), &req)
	if err != nil {
		logger.Logger.Error("Failed to execute agent", zap.Error(err))
		response.InternalServerError(c, "Failed to execute agent")
		return
	}

	response.Success(c, execution)
}

// GetAgentStatus 获取代理状态
func (h *AgentHandler) GetAgentStatus(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "Invalid agent ID")
		return
	}

	agent, err := h.agentService.GetAgent(c.Request.Context(), uint(id))
	if err != nil {
		response.NotFound(c, "Agent not found")
		return
	}

	response.Success(c, gin.H{
		"agent_id": agent.ID,
		"status":   agent.Status,
		"name":     agent.Name,
		"type":     agent.Type,
	})
}

// CreateTask 创建任务
func (h *AgentHandler) CreateTask(c *gin.Context) {
	// TODO: 实现任务创建逻辑
	response.Success(c, gin.H{"message": "Task creation not implemented yet"})
}

// ListTasks 获取任务列表
func (h *AgentHandler) ListTasks(c *gin.Context) {
	// TODO: 实现任务列表逻辑
	response.Success(c, gin.H{"message": "Task listing not implemented yet"})
}

// GetTask 获取任务详情
func (h *AgentHandler) GetTask(c *gin.Context) {
	// TODO: 实现任务详情逻辑
	response.Success(c, gin.H{"message": "Task details not implemented yet"})
}

// UpdateTask 更新任务
func (h *AgentHandler) UpdateTask(c *gin.Context) {
	// TODO: 实现任务更新逻辑
	response.Success(c, gin.H{"message": "Task update not implemented yet"})
}

// DeleteTask 删除任务
func (h *AgentHandler) DeleteTask(c *gin.Context) {
	// TODO: 实现任务删除逻辑
	response.Success(c, gin.H{"message": "Task deletion not implemented yet"})
}
